﻿@using Single_SignOn.ResX
@model Single_SignOn.Areas.Handheld.Models.ChangePassword

@{
    ViewBag.Title = "Index";
    Layout = "~/Areas/Handheld/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("OnIndex", "ChangeSword", new AjaxOptions { HttpMethod = "POST" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    
    <div id="content">
        <table style="width: 100%; height: 100%;">
            <tr>
                <td style="width: 100%; text-align: center; vertical-align: middle;">
                    <div>
                        <fieldset>
                            <legend style="width: 100%; text-align: center; vertical-align: middle;">
                                <h5>@ResourceTools.GetResource("Resource", "ChangePasswordForm")</h5>
                            </legend>
                            <ol>
                                <li >
                                    @Html.LabelFor(m => m.UserName)<div class="error">
                                        <div class="error-username"></div>
                                    </div></li>
                                <li>
                                    @Html.TextBoxFor(m => m.UserName)</li>
                                <li >
                                    @Html.LabelFor(m => m.OldPassword)<div class="error">
                                        <div class="error-oldpassword"></div>
                                    </div></li>
                                <li>
                                    @Html.PasswordFor(m => m.OldPassword)</li>
                                <li >
                                    @Html.LabelFor(m => m.NewPassword)<div class="error">
                                        <div class="error-newpassword"></div>
                                    </div></li>
                                <li>
                                    @Html.PasswordFor(m => m.NewPassword)</li>
                                <li >
                                    @Html.LabelFor(m => m.ConfirmPassword)<div class="error">
                                        <div class="error-confirmpassword"></div>
                                    </div></li>
                                <li>
                                    @Html.PasswordFor(m => m.ConfirmPassword)</li>
                                <p></p>
                                <div style="text-align: center;" class="error">
                                    <div class="error-summary"></div>
                                </div>
                            </ol>
                            <p></p>
                            <button type="button" name="Save" id="Save" style="width: 100px;" value="Save" title = "@ResourceTools.GetResource("Resource", "Submit")">
                                <img src="~/Images/asset.gif" alt=" @ResourceTools.GetResource("Resource", "Submit") "/>
                                &nbsp; @ResourceTools.GetResource("Resource", "Submit")
                            </button>
                            <button type="button" name="Cancel" id="Cancel" style="width: 100px;" value="Cancel" title = "@ResourceTools.GetResource("Resource", "Back")">
                                <img src="~/Images/Delete.png" alt=" @ResourceTools.GetResource("Resource", "Back") "/>
                                &nbsp; @ResourceTools.GetResource("Resource", "Back")
                            </button>
                        </fieldset>
                    </div>
                </td>
            </tr>
        </table>
    </div>
    <div class="display">
        <script type="text/javascript">
            $(document).ready(function () {

                $('#Save').click(function () {
                    debugger;
                    var valid = true;
                    //*
                    $(".error-summary").html("");
                    $(".error-username").html("");
                    $(".error-newpassword").html("");
                    $(".error-oldpassword").html("");
                    $(".error-confirmpassword").html("");
                    //*
                    if ($("#UserName").val() == "") {
                        $(".error-username").html(" @ResourceTools.GetResource("Resource", "UsernameRequired") ");
                        valid = false;
                    }
                    if ($("#OldPassword").val() == "") {
                        $(".error-oldpassword").html(" @ResourceTools.GetResource("Resource", "OldPasswordRequired") ");
                        valid = false;
                    }

                    if ($("#NewPassword").val() == "") {
                        $(".error-newpassword").html(" @ResourceTools.GetResource("Resource", "NewPasswordRequired") ");
                        valid = false;
                    }

                    if ($("#ConfirmPassword").val() == "") {
                        $(".error-confirmpassword").html(" @ResourceTools.GetResource("Resource", "ConfirmPasswordRequired") ");
                        valid = false;
                    }

                    if ($("#NewPassword").val() != $("#ConfirmPassword").val()) {
                        $(".error-confirmpassword").html(" @ResourceTools.GetResource("Resource", "CompareConfirmPassword") ");
                        valid = false;
                    }

                    if (valid == true) {
                        var oChangnPasswordDataObj = {
                            "Username": $("#UserName").val(),
                            "OldPassword": $("#OldPassword").val(),
                            "NewPassword": $("#NewPassword").val(),
                            "ConfirmPassword": $("#ConfirmPassword").val()
                        };
                        //debugger;
                        $.ajax({
                            url: '@Url.Action("Confirm", "ChangeSword")',
                            type: "POST",
                            dataType: "json",
                            contentType: "application/json; charset=utf-8",
                            data: JSON.stringify(oChangnPasswordDataObj),
                            success: function (data, textStatus, xhr) {
                                //At this point I would like to redirect
                                if (data.exception == "") {
                                    window.location.href = data.redirectToUrl;
                                }
                                else {
                                    //*
                                    $("#Password").val("");
                                    $(".error-username").html("");
                                    $(".error-password").html("");
                                    //*
                                    $(".error-summary").html("");
                                    $(".error-summary").html(data.exception);
                                }
                            },
                            error: function (xhr, textStatus, errorThrown) {
                                //*
                                $("#Password").val("");
                                $(".error-username").html("");
                                $(".error-password").html("");
                                //*
                                $(".error-summary").html(xhr.responseText);
                            }
                        });

                        return false;
                    }
                });

                // Back
                $('#Cancel').click(function () {
                    /*
                    $("#UserName").val("");
                    $("#OldPassword").val("");
                    $("#NewPassword").val("");
                    $("#ConfirmPassword").val("");
                    $(".error-summary").html("");
                    $(".error-username").html("");
                    $(".error-newpassword").html("");
                    $(".error-oldpassword").html("");
                    $(".error-confirmpassword").html("");
                    */
                    window.location.href = document.location.href = $('#BackUrl').attr('href');
                });
            });

        </script>
        <a id="BackUrl" href="@Html.AttributeEncode(Url.Action("Index", "Login"))" style="display:none;"></a>
    </div>
}

